# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import xz_12306_class,db_pymsql
import json
import time,datetime
from django.shortcuts import render,redirect
from models import Order
from station_names import city,city2

data=[]

def search(request):#获取车辆信息
    if request.method =="GET":
        return render(request, "search.html",{'train_date':(datetime.datetime.now()+datetime.timedelta(days=1)).strftime('%Y-%m-%d')})
    else:
        train_date=request.POST.get("train_date")
        train_start = request.POST.get("dp")
        train_arrive = request.POST.get("ar")
        seat = request.POST.getlist("seat")
        if not (city.get(train_start) and city.get(train_arrive)):
            return render(request, "search.html",{'train_date':(datetime.datetime.now()+datetime.timedelta(days=1)).strftime('%Y-%m-%d'),'error':'站名输入不正确!'})
        train= xz_12306_class.Train()
        train.getTrain(train.CLeftTicketUrl,train_date, city[train_start], city[train_arrive])
        html = train.getTraindata
        return render(request,"search.html",{"train":html,"train_start":train_start,"train_arrive":train_arrive,"train_date":train_date})
def logins(request):
    if request.method =="GET":
        seat=request.GET.getlist('seat')
        train_no=request.GET.get('train_no')
        train_date=request.GET.get('train_date')
        start_station=request.GET.get('start_station')
        arrive_station=request.GET.get('arrive_station')
        start_time=request.GET.get('start_time')
        arrive_time=request.GET.get('arrive_time')
        #print seat,train_no
        return render(request,"login.html")
    else:
        username = request.POST.get("username")
        password = request.POST.get("password")
        seat=request.GET.get('seat')
        train_no=request.GET.get('train_no')
        train_date=request.GET.get('train_date')
        start_station=request.GET.get('start_station')
        arrive_station=request.GET.get('arrive_station')
        start_time=request.GET.get('start_time')
        arrive_time=request.GET.get('arrive_time')
        if not (username and password):return render(request,"login.html",{'error':'12306用户名或密码不能为空!','username':username})
        train= xz_12306_class.Train()
        print '实例化Train ID:',id(train)
        train.username=username
        train.password=password
        l=train.code()
        if l !=True:return render(request,"login.html",{'error':l,'username':username})
        login_user=train.login_user
        train.getPassInfo()
        return render(request,"passenger.html",{'list2':train.list2,'username':username,'password':password,'seat':seat,'train_no':train_no,'train_date':train_date,'start_station':start_station,'arrive_station':arrive_station,'start_time':start_time,'arrive_time':arrive_time,'login_user':login_user})

        return render(request,"login.html")
        #return render(request, "login.html",{'username':username} )
def makeorder(request):
    if request.method=="POST":
        select=request.POST.getlist('select')
        username=request.POST.get('username')
        password=request.POST.get('password')
        seat=request.POST.get('seat')
        train_no=request.POST.get('train_no')
        train_date=request.POST.get('train_date')
        start_station=request.POST.get('start_station')
        arrive_station=request.POST.get('arrive_station')
        start_time=request.POST.get('start_time')
        arrive_time=request.POST.get('arrive_time')
        login_user=request.POST.get('login_user')

        if select:
            p=Order()
            for i in select:
                print i
                p.passenger+=i+','
            p.passenger=p.passenger[:-1]
            p.order_id=time.strftime('%Y%m%d%H%M%S')
            p.username=username
            p.login_user=login_user
            p.password=password
            p.train_date=train_date
            p.train_no=train_no
            p.seat=seat
            p.start_time=start_time
            p.arrive_time=arrive_time
            p.start_station=start_station
            p.arrive_station=arrive_station
            p.state=0
            p.update_time=time.strftime('%Y-%m-%d %H:%M:%S')
            p.save()
        orderlist=Order.objects.filter(username=p.username).all()
        #print orderlist[0]
        select_passg= request.GET.getlist('select')
        ','.join(select_passg)
        return render(request, "order.html",{'data':orderlist} )
    else:
        return render(request, "order.html",{'data':Order.objects.filter().all()} )



def index(request):
    return redirect('12306/search/')

def go(request):
    from django.shortcuts import HttpResponse
    import subprocess,os#,sys,io
    os.chdir(r'f:/Project/git/web/my12306/train')
    # if 'cp65001'!= sys.stdout.encoding != 'utf-8':sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')
    # if 'cp65001'!= sys.stderr.encoding != 'utf-8':sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf8')

    print '当前地址是:',os.path
    #print 'python x12306_class.py -u %s -p %s -tk %s -ar %s -d %s -o %s -ps %s -n %s -s %s'%(request.GET.get('username'),request.GET.get('password'),request.GET.get('start_station'),request.GET.get('arrive_station'),request.GET.get('train_date'),request.GET.get('train_date'),request.GET.get('passenger'),request.GET.get('train_no'),request.GET.get('seat'))
    sb=subprocess.Popen(u'python 12306_class.py -u %s -p %s -tk %s -ar %s -d %s -o %s -ps %s -n %s'%(request.GET.get('username'),request.GET.get('password'),request.GET.get('start_station'),request.GET.get('arrive_station'),request.GET.get('train_date'),request.GET.get('order_id'),request.GET.get('passenger'),request.GET.get('train_no')))
    #print sb.pid,sb.communicate()
    return render(request,'order.html',{'data':Order.objects.filter().all()})

def sql(request):
    if request.method=="POST":
        sql=request.POST.get('sql')
        dbs=db_pymsql.db_mysql()
        data=dbs.exe(sql,1)
        return render(request,'sql.html',{'title':dbs.title,'data':data,'sql':sql})
    else:
        return render(request,'sql.html')
def wx(request):
    return render(request,'wx.html')